package com.njit.mailm.core.service.impl;

import com.njit.mailm.commmon.utils.GUID;
import com.njit.mailm.core.bean.Classify;
import com.njit.mailm.core.bean.User;
import com.njit.mailm.core.dao.TreeDao;
import com.njit.mailm.core.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/10.
 */
@Service
public class TreeServiceImpl implements TreeService {

    @Autowired
    private TreeDao treeDao;

    public boolean addRoot(Classify classify) {
        try {
            treeDao.addRoot(classify);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public Classify getClassifyByName(String name, String userId) {

        return treeDao.getClassifyByName(name,userId);
    }

    public boolean updateRoot(Classify classify, String newName) {

        try {
            treeDao.updateRoot(classify,newName);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteRoot(Classify classify) {
        try {
            treeDao.deleteRoot(classify);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean addChildElement(String name, String parentName, User user) {
        String id = GUID.getGUID();
        Classify classify = new Classify();
        classify.setId(id);
        classify.setUser(user);
        classify.setRoot(false);
        classify.setName(name);

        try {
            treeDao.addElement(classify);
//            treeDao.updateParentChildId(parentName,user.getId(),id);
            treeDao.addChildId(parentName,user.getId(),id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public boolean updateChildElement(String name, String originName, User user)  {
        Classify classify = treeDao.getClassifyByName(originName, user.getId());
        try {
            treeDao.updateRoot(classify,name);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteChildElement(String name, User user) {
        Classify classify = treeDao.getClassifyByName(name, user.getId());
        try {
            treeDao.deleteRoot(classify);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public List<Classify> listAllElement(User user) {
        List<Classify> result = treeDao.listRootElement(user);
        return result;
    }

//    private void wrapElements(User user,Classify classify){
//        List<Classify> childs = classify.getChild();
//
//        if(childs != null && childs.size()> 0){
//            List<Classify> secondChilds = new ArrayList<Classify>();
//            for(Classify child: childs){
//                secondChilds.add(treeDao.getElements(user,child.getId()));
//                wrapElements(user,treeDao.getElements(user,child.getId()));
//            }
//            classify.setChild(secondChilds);
//        }else{
//            return ;
//        }
//    }
//
//    private List<Classify> listRootElement(User user){
//        return treeDao.listRootElement(user);
//    }

}
